/**
 * 
 */
package it.irondev.appturismo.action;

import it.irondev.appturismo.entity.Attractions;
import it.irondev.appturismo.entity.City;
import it.irondev.appturismo.entity.Gallery;
import it.irondev.appturismo.entity.Language;
import it.irondev.appturismo.filter.BaseFilter;
import it.irondev.appturismo.service.IAttractionService;
import it.irondev.appturismo.service.ICityService;
import it.irondev.appturismo.service.IGalleryService;
import it.irondev.appturismo.service.ILanguageService;
import it.irondev.appturismo.util.ActionHelper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * @author federicocerruto
 *
 */
@Controller("attractionAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AttractionAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1209610540205359781L;

	@Autowired
	IAttractionService attractionService;
	
	@Autowired
	ILanguageService serviceLanguage;

	@Autowired
	ICityService serviceCity;
	
	@Autowired
	IGalleryService galleryService;

	private Integer idGallery;
	private Long idAttraction;
	private List<Language> listLanguage;
	private List<City> listCity;
	private Attractions entityBean;
	private List<Attractions> attractions;
	private String stringaSearchLanguage;
	
	
	/* (non-Javadoc)
	 * @see it.irondev.appturismo.action.IBaseAction#list()
	 */
	public String list() {
		listLanguage = serviceLanguage.getAllLanguages();
		setStringaSearchLanguage(ActionHelper.buildStringSearch(listLanguage)); 
		return "listAttractions";
	}

	
	public String listJson() {
		attractions = attractionService.getAllAttractions();
		return "listAttractionJson";
	}
	

	public String nuovo() {
		listCity = serviceCity.getAllCity();
		listLanguage = serviceLanguage.getAllLanguages();
		return "newAttraction";	
	}

	public String edit() {
		listLanguage = serviceLanguage.getAllLanguages();
		listCity = serviceCity.getAllCity();
		entityBean = attractionService.getAttractionById(idAttraction.intValue());
		return "editAttraction";	
	}
	
	public String detailMobile() {
		entityBean = attractionService.getAttractionById(idAttraction.intValue());
		return "detailAttractionMobile";
	}

	
	public void delete() {
		if (getOper().equalsIgnoreCase("del")) {
			int idEntity = Integer.parseInt(getId());
			attractionService.deleteAttraction(idEntity);
		}
	}

	
	public String update() {
		attractionService.updateAttraction(entityBean);
		return "updateAttraction";
	}

	
	public String add() {
		attractionService.addAttraction(entityBean);
		return "addedAttraction";
	}
	
	
	public String addGallery() {
		Gallery tmpGallery = new Gallery();
		galleryService.addGallery(tmpGallery);
		idGallery = tmpGallery.getIdgallery();
		entityBean = attractionService.getAttractionById(idAttraction.intValue());
		entityBean.setGallery(galleryService.getGalleryById(idGallery));
		attractionService.updateAttraction(entityBean);
		return "addAttractionGallery";
	}
	
	/* 
	 * START MOBILE ACTION
	 */
	
	/**
	 * @return the idGallery
	 */
	public Integer getIdGallery() {
		return idGallery;
	}


	/**
	 * @param idGallery the idGallery to set
	 */
	public void setIdGallery(Integer idGallery) {
		this.idGallery = idGallery;
	}


	public String listMobile() {
		String ln = getLocale().getLanguage();
		BaseFilter filter = new BaseFilter();
		filter.setIdlanguage(serviceLanguage.getLanguageByPrefix(ln).getIdlanguage());
		filter.setFavouriteCity(getFavouriteCity());
		attractions = attractionService.getAttractionsByFilter(filter);
		return "listAttractionMobile";
	}
	
	
	public Long getIdAttraction() {
		return idAttraction;
	}

	public void setIdAttraction(Long idAttraction) {
		this.idAttraction = idAttraction;
	}

	public List<Language> getListLanguage() {
		return listLanguage;
	}

	public void setListLanguage(List<Language> listLanguage) {
		this.listLanguage = listLanguage;
	}

	public List<City> getListCity() {
		return listCity;
	}

	public void setListCity(List<City> listCity) {
		this.listCity = listCity;
	}

	public Attractions getEntityBean() {
		return entityBean;
	}

	public void setEntityBean(Attractions entityBean) {
		this.entityBean = entityBean;
	}

	public List<Attractions> getAttractions() {
		return attractions;
	}

	public void setAttractions(List<Attractions> attractions) {
		this.attractions = attractions;
	}

	public String getStringaSearchLanguage() {
		return stringaSearchLanguage;
	}

	public void setStringaSearchLanguage(String stringaSearchLanguage) {
		this.stringaSearchLanguage = stringaSearchLanguage;
	}


}
